package com.shimao.celnet.payment.service.impl;

import com.github.pagehelper.PageHelper;
import com.shimao.celnet.payment.form.BInvoiceCompanyForm;
import com.shimao.celnet.payment.model.BInvoiceCompany;
import com.shimao.celnet.payment.service.BInvoiceCompanyService;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;

/**
 * 开票公司管理
 * Created by ensure on 2017/12/3.
 */
@Service
public class BInvoiceCompanyServiceImpl extends BaseServiceImpl<BInvoiceCompany> implements BInvoiceCompanyService {
    protected Logger logger = Logger.getLogger(this.getClass());

    /**
     * 分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public BInvoiceCompanyForm getByPage(BInvoiceCompanyForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(BInvoiceCompany.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(formBean.getEstateGuid())) {
            //小区
            criteria.andEqualTo("estateGuid",  formBean.getEstateGuid() );
        }
        if (StringUtil.isNotEmpty(formBean.getSellerTaxNo())) {
            //纳税人识别号
            criteria.andLike("sellerTaxNo",  "%"+formBean.getSellerTaxNo()+"%" );
        }
        if (StringUtil.isNotEmpty(formBean.getCompanyName())) {
            //公司名称
            criteria.andLike("companyName", "%"+ formBean.getCompanyName()+"%" );
        }

        //分页查询
        PageHelper.startPage(pageNum, length);
        List<BInvoiceCompany> tableList = getByExample(example);
        formBean.setPageInfo(tableList);
        return  formBean;
    }

    /**
     *新增保存
     * @return
     */
    @Override
    public int save(BInvoiceCompany sBean){
        int retCnt=0;
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        sBean.setCreatedUserGuid(loginUserGuid);
        sBean.setGuid(getGuid());//guid
        retCnt = super.save(sBean);

        return retCnt;
    }

    /**
     *修改保存
     * @return
     */
    @Override
    public int update(BInvoiceCompany sBean){
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        int retCnt = super.update(sBean);

        return retCnt;
    }

    /**
     * 根据小区sfguid，取得开票公司,销方单位税号
     * @param estateSFGuid
     * @return
     */
    @Override
    public BInvoiceCompany getCompanyByEstateSFGuid(String estateSFGuid){

        Example example = new Example(BInvoiceCompany.class);
        Example.Criteria criteria = example.createCriteria();
            //小区SFGUID
        criteria.andEqualTo("estateSfGuid", estateSFGuid );
        List<BInvoiceCompany> tableList = getByExample(example);
        if(tableList.size()>0){
            return tableList.get(0);
        }
        return null;
    }

    /***
     * 保存对象
     * @param bInvoiceCompany
     */
    @Override
    public void saveByObject(BInvoiceCompany bInvoiceCompany) {
       super.save(bInvoiceCompany);
    }
}
